﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Global Role Mangement
// Created By:     TingRong Shi (trshi@wicresoft.com)
// Created Date:   2011-2-19
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using ASBCC.Database.EntityFramework;
using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;


namespace ASB.CC.Business.ACR
{
    public static class CustomerSearchHelper
    {
        /// <summary>
        /// 获取所有可选类别
        /// </summary>
        /// <returns></returns>
        public static IEnumerable GetCategory()
        {
            using (var dc = new CCEntities())
            {
                return dc.IssueTracker_ProjectCategories.ToArray();
            }
        }

        /// <summary>
        /// 获取创建人
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string QueryCreatorName(string name)
        {
            using (var dc = new CCEntities())
            {
                var users = (from a in dc.IssueTracker_Users
                             where a.UserName.ToLower().Contains(name.ToLower())
                             select a).ToArray();
                string userIDs = "";
                if (users != null)
                {
                    foreach (var user in users)
                    {
                        userIDs += user.UserId;
                        userIDs += ",";
                    }
                }
                if (userIDs.Length > 0)
                    userIDs = userIDs.Substring(0, userIDs.Length - 1);
                return userIDs;
            }
            
        }

        /// <summary>
        /// 根据ISSUEID和ACRID查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="acrID"></param>
        /// <returns></returns>
        public static string GetIssuesByConditions(string id, string acrID)
        {
            using (var dc = new CCEntities())
            {
                var issues = dc.SP_ACISSearch(id, acrID).ToArray();
                string issueIDs = "";
                if(issues!=null&& issues.Count()>0)
                {
                    foreach(var issue in issues)
                    {
                        issueIDs += issue.IssueId.ToString();
                        issueIDs += ",";
                    }
                }
                if (issueIDs.Length > 0)
                    issueIDs = issueIDs.Substring(0, issueIDs.Length - 1);
                return issueIDs;
            }
        }

        /// <summary>
        /// 获取UserName
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetUserNameByID(string id)
        {
            int userid = int.Parse(id);
            using (var dc = new CCEntities())
            {
                var user = dc.IssueTracker_Users.FirstOrDefault(p => p.UserId == userid);
                return user != null ? user.DisplayName : "";
            }

        }

        /// <summary>
        /// 根据CategoryID获取类别名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetCategoryNameByID(string id)
        {
            int categoryid = int.Parse(id);
            using (var dc = new CCEntities())
            {
                return dc.IssueTracker_ProjectCategories.First(p => p.CategoryId == categoryid).CategoryName;
            }
        }

        public static string GetCustomerFieldValue(int issueID, int fieldID)
        {
            using (var dc = new CCEntities())
            {
                return dc.IssueTracker_ProjectCustomFieldValues.First(p => p.IssueId == issueID && p.CustomFieldId == fieldID).CustomFieldValue;
            }
        }


    }
}
